/* Class to manage sprites for the game and it's special brand of things */
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace dungeon_brawler.classes
{
    
    
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SpriteManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        Player player;
        List<Enemy> enemies = new List<Enemy>();

        public String GetPlayerInfo()
        {
            if (player != null)
                return player.ToString();
            else
                return "Player not found";
        }

        public String GetEnemyInfo()
        {
            if (enemies.Count != 0 && enemies != null)
                return enemies[0].ToString();
            else
                return "Enemy not found";
        }

        public Vector2 GetPlayerPosition()
        {
            return player.Position;
        }

        public SpriteManager(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            // Create the player
            player = new Warrior(1, new Vector2(0, 0),Game.Content.Load<Texture2D>(@"warrior"));
            // Cretae enemies
            enemies.Add(new Enemy1(1, new Vector2(250, 250), Game.Content.Load<Texture2D>(@"warrior"),player));

            
            base.LoadContent();
        }
                

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            player.Update(gameTime, Game.Window.ClientBounds);
            foreach (Enemy enemy in enemies)
            {
                enemy.Update(gameTime, Game.Window.ClientBounds);
            }
            
            base.Draw(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
                SpriteSortMode.FrontToBack, SaveStateMode.None);
            //// Start Draw
            
            player.Draw(spriteBatch);
   
            foreach (Enemy enemy in enemies)
            {
                enemy.Draw(spriteBatch);
                // Chk for collisons
                if (player.collisionRect.Intersects(enemy.collisionRect))
                {
                    // Enemy is hit
                    enemy.Spd = 0;
                    break;
                }
                
                // Chk for collisons
                if (player.atkRect.Intersects(enemy.collisionRect)&&player.IsAtk)
                {
                    // Enemy is hit
                    enemies.Remove(enemy);
                    break;
                }

            }

            //// End Draw
            spriteBatch.End();


            base.Draw(gameTime);
        }

    }
}